const fs = require('fs')
const path = require('path')

const origin = '/Users/shanke/Documents/appdata/torrentmanager/torrents/111.jpeg'
const target = '/Users/shanke/Documents/appdata/torrentmanager/linked/Movies/Season 1/111.jpeg'

const replace = false

async function main() {
    if (await exists(origin)) {
        if (await exists(target) && !replace) {
            console.log('目标已存在')   
        } else {
            fs.rmSync(target, { force: true })
            fs.mkdirSync(path.dirname(target), { recursive: true })
            // 硬连接
            fs.linkSync(origin, target)
            // 移动
            // fs.renameSync(origin, target)
        }
    } else {
        console.log('原文件不存在')
    }
}

function exists(file) {
    return new Promise((resolve,reject)=>{
        fs.access(file, err => {
            if(err) {
                if (err.code === 'ENOENT') {
                    resolve(false)
                } else {
                    reject(err)
                }
            } else {
                resolve(true)
            }
        })
    })
}

// main()

console.log('C:\\1\\2\\3', 'C:\\1\\2\\3'.indexOf(path.win32.sep))
console.log('/mnt/user'.indexOf(path.posix.sep))

// fs.stat(origin, (err, stats) => {
//     console.log(stats)
// })